package net.yla.smartbus.core.constants;

import com.alibaba.fastjson.annotation.JSONType;
import net.yla.smartbus.core.exception.EnumNotFoundException;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author KongXianDe
 * @date 2019/7/10 16:04
 */
@JSONType(serializeEnumAsJavaBean = true)
public enum RequestMethodEnum {

    /**
     * POST
     */
    POST(0, "POST"),
    /**
     * PUT
     */
    PUT(1, "PUT"),
    /**
     * DELETE
     */
    DELETE(2, "DELETE"),
    /**
     * GET
     */
    GET(3, "GET");

    private int code;
    private String desc;
    private String name;

    private RequestMethodEnum(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    private static final Map<Integer, RequestMethodEnum> BY_VALUE_MAP = new LinkedHashMap<>();

    static {
        for (RequestMethodEnum requestMethodEnum : values()) {
            BY_VALUE_MAP.put(requestMethodEnum.getCode(), requestMethodEnum);
        }
    }

    /**
     * 根据code查询对应的外卖类型枚举
     *
     * @param code dode
     * @return RequestMethodEnum
     * @throws EnumNotFoundException
     */
    public static RequestMethodEnum convertCode2Enum(int code) throws EnumNotFoundException {
        if (!BY_VALUE_MAP.containsKey(code)) {
            throw new EnumNotFoundException("枚举对应的code" + code + "不存在");
        }
        return BY_VALUE_MAP.get(code);
    }

    /**
     * 根据枚举name查询对应的外卖类型枚举
     *
     * @param str 枚举的name
     * @return RequestMethodEnum
     * @throws EnumNotFoundException
     */
    public static RequestMethodEnum convertStr2Enum(String str) throws EnumNotFoundException {
        try {
            return valueOf(str);
        } catch (Exception e) {
            throw new EnumNotFoundException("枚举对应常量" + str + "不存在");
        }
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getName() {
        return this.name();
    }

}
